require("./model")
const config = require("./config")
const koa = require("koa");
const router = new require("@koa/router")({prefix:"/api"});
const body_parser = require("koa-bodyparser");
const session = require("koa-session");
const path = require("path");
const cors = require("koa-cors");
const app = new koa();
const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer(app.callback());
const koaBody = require('koa-body');
const koa_static = require("koa-static");
const socket = require("./socket");
const fs = require("fs");

// 引入路由
const user = require("./router/user_router")
const group = require("./router/group_router")
const dynamic = require("./router/dynamic_router")
const notify = require("./router/notify_router")

const conf = {
  key: "koa.sess"
};

app.use( 
  cors({
    origin: config.origins,
    credentials:true
  })
);
app.use(koa_static(path.join(__dirname,"uploads")));
app.use(session(conf, app));
// app.use(body_parser());

// 创建Socket服务器
socket.createSockerServer(Server,httpServer);

socket.connect();

app.use(koaBody({
  multipart: true,
  formidable: {
      maxFileSize: 5*1024*1024    // 设置上传文件大小最大限制，默认2M
  }
}));

router.use(user.routes(),user.allowedMethods())
.use(group.routes(),group.allowedMethods())
.use(dynamic.routes(),dynamic.allowedMethods())
.use(notify.routes(),notify.allowedMethods())

app.use(router.routes()).use(router.allowedMethods());

httpServer.listen(config.port);
